package org.lookahead.parent.common.utils;


import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lookahead.parent.apidoc.enums.SmsTypeEnum;

import java.util.Map;

/**
 * 阿里短信平台工具类
 */
@Slf4j
public class AliSMSUtil {

    public static final String END_POINT = "dysmsapi.aliyuncs.com";
    public static final Integer SMS_CODE_SUCCESS = 200;

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId     ak
     * @param accessKeySecret sk
     * @return Client
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = END_POINT;
        return new Client(config);
    }

    /**
     * 使用STS鉴权方式初始化账号Client，推荐此方式。
     *
     * @param accessKeyId     ak
     * @param accessKeySecret sk
     * @param securityToken   token
     * @return Client
     */
    public static Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                // 必填，您的 Security Token
                .setSecurityToken(securityToken)
                // 必填，表明使用 STS 方式
                .setType("sts");
        // 访问的域名
        config.endpoint = END_POINT;
        return new Client(config);
    }

    /**
     * 发送短信
     *
     * @param prop   阿里云短信平台配置
     * @param mobile 手机号
     * @param param  模板参数内容
     * @return boolean
     */
    public static Boolean doSend(SmsProperties prop, String mobile, SmsTypeEnum typeEnum, Map<String, String> param) {
        try {
            Client client = AliSMSUtil.createClient(prop.getAk(), prop.getSk());
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(prop.getSignName())
                    .setTemplateCode(typeEnum.getTemplateId())
                    .setPhoneNumbers(mobile)
                    .setTemplateParam(JSON.toJSONString(param));
            RuntimeOptions runtime = new RuntimeOptions();
            log.info("调用阿里云短信平台,手机号:{},模板:{},签名:{}, 内容{}",
                    mobile, typeEnum.getTemplateId(), prop.getSignName(), JSON.toJSONString(param));
            SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info(JSON.toJSONString(resp));
            if (SMS_CODE_SUCCESS.equals(resp.statusCode)) {
                return true;
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
        return false;
    }

    /**
     * 测试方法
     *
     * @param args 参数
     */
    public static void main(String[] args) {
        String mobile = "18601377252";
        ImmutableMap<String, String> param = ImmutableMap.of("code", "1234");
        SmsProperties smsProperties = SmsProperties.builder()
                .ak("LTAI5t7Z9hB15fDKie46inVA")
                .sk("k22fkpNUTNovgG16Hq59POBt4FkTPU")
                .signName("走向未来")
                .build();
        Boolean send = AliSMSUtil.doSend(smsProperties, mobile, SmsTypeEnum.SMS_CODE, param);
        System.out.println(send);
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SmsProperties {
        private String ak;
        private String sk;
        private String signName;
        private String signYingtong;
    }


}
